package com.larkin.utils.config;

import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
 * Created by liujin on 2019/5/26.
 *
 * @description TODO
 */
public class WebInitializer implements WebApplicationInitializer {
    @Override
    public void onStartup(ServletContext servletContext) throws ServletException {

        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(DemoConfig.class);
        servletContext.addListener(new ContextLoaderListener(ctx));

        ServletRegistration.Dynamic registration = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        registration.setLoadOnStartup(1);
        registration.addMapping("/*");

        /*servletContext.setInitParameter("log4jConfigLocation", "classpath:config/fbs-log4j.xml");
        servletContext.addListener(Log4jConfigListener.class);


        AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
        ctx.register(MvcConfig.class);
        ctx.setServletContext(servletContext);

        Properties properties = new Properties();
        try {
            properties.load(this.getClass().getResourceAsStream("/env.properties"));
            for (Object object : properties.keySet()) {
                String key = (String) object;
                String value = properties.getProperty(key);
                servletContext.setInitParameter(key, value);
                logger.info("load context init param from resources, key: [{}], value: [{}]", key, value);
            }
        } catch (Exception e) {
            logger.error("[initParam] load resources error", e);
        }

        ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
        servlet.addMapping("/*");
        servlet.setLoadOnStartup(1);

        ServletRegistration.Dynamic registration = servletContext.addServlet("CXFServlet", new CXFServlet());
        registration.setLoadOnStartup(2);
        registration.addMapping("/services/*");*/
    }
}
